package cn.holy.app.sztxc.login;

import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.qmuiteam.qmui.util.QMUIKeyboardHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import javax.inject.Inject;

import butterknife.BindView;
import cn.holy.app.sztxc.R;
import cn.holy.app.sztxc.activity.base.BaseActivity;
import cn.holy.app.sztxc.domain.LoginFormBean;
import cn.holy.app.sztxc.domain.UserInfoBean;
import cn.holy.app.sztxc.login.module.DaggerLoginComponent;
import cn.holy.app.sztxc.login.view.LoginViewModel;
import cn.holy.app.sztxc.net.callback.LoadingCallback;

@Route(path = "/app/login")
public class LoginActivity extends BaseActivity {

    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.cb_agree)
    CheckBox cbAgree;
    @BindView(R.id.btn_login)
    QMUIRoundButton btnLogin;

    @Inject
    LoginViewModel loginViewModel;


    @Override
    protected void initView() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        DaggerLoginComponent.create().inject(this);
    }

    @Override
    protected void initData() {

        LoginFormBean loginFormBean = loginViewModel.loadLoginInfo();
        etUsername.setText(loginFormBean.getUsername());
        etPassword.setText(loginFormBean.getPassword());
//
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QMUIKeyboardHelper.hideKeyboard(etPassword);
                loginViewModel.login(etUsername.getText().toString(), etPassword.getText().toString(), cbAgree.isChecked(), new LoadingCallback<UserInfoBean>() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onError(String msg) {

                    }

                    @Override
                    public void onSuccess(UserInfoBean userInfoBean) {
                        finish();
                    }

                    @Override
                    public void onCompleted() {

                    }
                });
            }
        });
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_login);
    }

}
